﻿using System;
using UnityEngine;

/// <summary>
/// When an agent has this movement behaviour it will actively desire to move
/// to the location of the target.
/// </summary>
public class SeekMovementBehaviour : DirectMovementBehaviour
{
    /// <summary>
    /// Constructor for SeekMovementBehaviour instances.
    /// </summary>
    /// <param name="movementBehaviour">The movement behaviour to decorate.</param>
    /// <param name="agent">The GameObject that desires this movement behaviour.</param>
    /// <param name="target">The target of this movement behaviour.</param>
    /// <param name="radius">The radius at which this behaviour is completed.</param>
    public SeekMovementBehaviour(AbstractMovementBehaviour movementBehaviour, GameObject agent, GameObject target, float radius)
         : base(movementBehaviour, agent, target, radius) { }

    /// <summary>
    /// The velocity desired by this movement behaviour.
    /// </summary>
    /// <returns>The optimal velocity vector to accomplish this movement behaviour.</returns>
    public override Vector2 CalculateDesiredVelocity()
    {
        var desiredVelocity = CalculateMaximumVelocity(agent.transform.position, target.transform.position);

        var distance = Vector2.Distance(agent.transform.position, target.transform.position);
        var radiusFactor = Mathf.Clamp(distance / radius, 0.0f, 1.0f);
        desiredVelocity *= radiusFactor;

        if (distance < 0.3f) desiredVelocity = Vector2.zero;

        Debug.DrawRay(agent.transform.position, desiredVelocity * 10, Color.green);
        return  desiredVelocity + movementBehaviour.CalculateDesiredVelocity();
    }
}
